#include "includes.h"

uint8_t ir_value = 0;
uint8_t last_ir_value = 0;
uint8_t scoreflag = 0;
uint8_t counter = 0;
void adc128_init()
{
	DDRF = 0x00;
	// Use VACC as voltage reference and use ADC0 as input
	ADMUX = (1<<REFS0);
	// Enabler ADC og setter ADCfrekvens lik klokke/128.
	ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}


void adc_poll()
{
	char temp[10];

	if (counter==60)
	{	
		scoreflag = 0;
		counter = 0;
	}
	if(scoreflag==1)
	{	
		counter++;
		return;
	}

	uint8_t dummy;
	last_ir_value=ir_value;
	char samples[4];

	while (counter!=3) 
	{
		set_bit(ADCSRA,ADSC);
		_delay_us(10);
		samples[counter]=ADCL;
		dummy = ADCH;
		counter++;
	}
	counter=0;
	ir_value = (samples[0]+samples[1]+samples[2])/3;

	if(ir_value<(last_ir_value-20) && ir_value<16)
	{
		scoreflag = 1;
		game_add_point();	
	}
	
/*	USART_print(itoa(ir_value,temp,10));
	USART_print(" - ");
	USART_print(itoa(last_ir_value,temp,16));
	USART_print("\r\n");
*/
}
